package com.microblogforsim.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.microblogforsim.R;
import com.microblogforsim.activity.PreviewImgActivity;
import com.microblogforsim.model.Topics;
import com.microblogforsim.utils.WindowSize;

import java.io.Serializable;
import java.util.List;

public class MyGridViewAdapter extends BaseAdapter {
    Context context;
    int num;
    int col;
    private List<Topics.DataBean.ImgBean> imglist = null;


    public MyGridViewAdapter(Context context, int num, int col, List<Topics.DataBean.ImgBean> imgBean) {
        this.context = context;
        this.num = num;
        this.col = col;
        this.imglist = imgBean;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return num;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView img = new ImageView(context);
        //图片展示方式
        img.setScaleType(ImageView.ScaleType.CENTER_CROP);
        int width = WindowSize.getWidth(context);// 获取屏幕宽度
        int height = 0;
        width = width / col;// 对当前的列数进行设置imgView的宽度
        height = width;
        img.setLayoutParams(new AbsListView.LayoutParams(width, height));
        if(this.imglist.size()>0){


            Glide.with(context)
                    //加载地址this.imglist.get(position).getSrc()+
                    .load(this.imglist.get(position)
                            .getSrc()+"")
                    //加载失败时，设置默认的图片
                    .placeholder(R.drawable.loading)
                    //显示缩略图
                    .thumbnail(0.2f)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    //显示的位置
                    .into(img);
        }else{
            Log.d("TAG", "getView: 无图片");
        }

        img.setFocusable(false);
        img.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View v) {
                //页面跳转
                 Intent intent = new Intent(context, PreviewImgActivity.class);
                 intent.putExtra("url",imglist.get(position).getSrc());
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("helpTopicImage", (Serializable)imglist);
//                bundle.putInt("position",position);
//                intent.putExtras(bundle);
               //  context.startActivity(intent);

            }
        });
        return img;
    }
}